<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */
function check_media_file ($content_id){
    global $db;
    if (!empty($content_id)){
        $sql="select count(*) from _content_media where content_id=$content_id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            return $row[0]>0;
        } else return FALSE;
    } else return FALSE;
}
function extract_media_file($content_id){
    global $db;
    if (!empty($content_id)){
        $sql="select b.filename 
        from _content_media a,_media_file b
        where a.content_id=$content_id and a.file_id=b.id";
        $result=$db->query($sql);
        $i=0;
        while ($row=$db->fetch_array($result)){
          if (!empty($row)){
            extract($row);
            $file[$i]=$filename;
          }  
          $i++;
        }
        return implode('<br/>',$file);  
    } else return "";
}
function list_table(){
    global $db,$page,$pagesize,$login_status,$user_id,$channel_id;
     
    if (empty($page)){
        $start=0;
        ;
    } else {
        $start=$page * $pagesize;
        
    }
    if (empty($channel_id)){
        $sql="select a.*,c.category_name,b.channel_name
        from _channel_contents a,_channel_channels b,_channel_category c
        where a.channel_id=b.id
        order by a.id limit $start,$pagesize "; 
    } else {
    $sql="select a.*,c.category_name,b.channel_name 
        from _channel_contents a,_channel_channels b,_channel_category c
        where a.channel_id=b.id and b.channel_category=c.id and a.channel_id=$channel_id
        order by a.id limit $start,$pagesize";
    }
    $result=$db->query($sql);
    //echo "<table id=\"table_box\" cellpadding=2 cellspacing=0><tr valign=\"top\">";
    $col=0;
    echo "<div align=\"right\"><a href=\"?mod=panel\">
        <img class=\"icon\" src=\"images/panel.png\" title=\"Back to Panel\"/></a>
<a href=\"?mod=panel&file=contents&action=add&channel_id=$channel_id\">
<img class=\"icon\" src=\"images/panel/add_media.png\" title=\"Add Channel Content\"/></a></div>        
<div id=\"channel_list\"><table width=\"100%\" cellspacing=0 cellpadding=2>
<tr><th>No</th><th>Title</th><th>Media</th><th>Action</th></tr>
";
    $i=0;
   
    while ($row=$db->fetch_array($result)){
        extract($row);
        if ($login_status){
        $editlink="";
        }
        $i++; 
        $scrap=substr($description,0, 50);
        if (check_media_file($id)){
            $media_note=  extract_media_file($id);
            
            
        } else {
            if (!empty($media_embed)){
                $media_note="media di embed dari sumber lain";
            } else
            $media_note="Tidak ada media di server";
        }
        echo "<tr ><td class=\"table_row_left\">$i</td>
        <td class=\"table_row\"><a href=\"#\" 
        title=\"$description\">$title</a></td><td class=\"table_row\">
        <a href=\"?mod=panel&file=contents&content_id=$id&action=play_media\">$media_note</a></td><td class=\"table_row\">
        <a href=\"?mod=panel&file=contents&action=upload&id=$id\">
    <img class=icon src=\"images/ic_upload_media.png\" title=\"Upload\" /></a>
        <a href=\"?mod=panel&file=contents&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a class=\"delete_confirm\" href=\"?mod=panel&file=contents&action=delete&id=$id\">
    <img class=icon src=\"images/delete.png\" title=\"Hapus\" /></a>
        </td>  </tr>
       ";
         
    }
     $jrow=mysql_num_rows($result);
    if ($jrow<1){
        echo "<tr><td colspan='4' class=\"table_end\">Tidak ada konten untuk Channel ini</td></tr>";
    }
    echo "</table></div>";
}
function edit_form(){
    global $db,$id;
    if (!empty($id)){
        $sql="select a.*,b.user_name 
        from _channel_contents a,_user_users b 
        where a.id=$id and a.user_create=b.id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            echo "<form action=\"?mod=panel&file=contents\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td>Id</td><td>$id<input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td>Channel Name</td><td>".  select_channel_list($channel_id)." </td></tr>
<tr><td>Title</td><td><input type=\"text\" name=\"title\" class=\"textbox\" size=50
                    value=\"$title\"/> </td></tr>
<tr><td>Media Embed</td><td><textarea name=\"media_embed\" 
                    class=\"textbox\" rows=5 cols=50>".htmlentities($media_embed)."</textarea> </td></tr>
<tr><td>Media File</td><td>".  select_media_file('media_file[]', '');
            
            echo "</td></tr>
<tr><td>Description</td><td><textarea name=\"description\" class=\"textbox\">$description</textarea> </td></tr>
<tr><td>Hits</td><td><input type=\"text\" name=\"hits\" class=\"textbox\" value='$hits'/> </td></tr>
<tr><td>Hit Like</td><td><input type=\"text\" name=\"hit_like\" class=\"textbox\" value='$hit_like'/> </td></tr>
<tr><td>Hit Dislike</td><td><input type=\"text\" name=\"hit_dislike\" class=\"textbox\" value='$hit_dislike'/> </td></tr>
<tr><td>Date Create</td><td>$date_create </td></tr>
<tr><td>User Create</td><td>$user_name</td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
        }
    }
}
function edit_process(){
    global $db,$id,$channel_id,$title,$media_embed,$media_file,$description,$hits,
            $hit_like,$hit_dislike,$date_create,$user_create;
    if (!empty($id)){
        $sql="update _channel_contents set channel_id='$channel_id',title='$title',
        media_embed='$media_embed',description='$description',
        hits=$hits,hit_like=$hit_like,hit_dislike=$hit_dislike
        where id=$id";
        $db->query($sql);
        if (is_array($media_file)){
            for ($i=0;$i<count($media_file);$i++){
            $sql="insert into _content_media 
               values ({$media_file[$i]},$id)";
           // echo $sql.";";
            $db->query($sql);
            
            }
        }
        refresh_to("?mod=panel&file=channel", "Kembali ke daftar Channel", 1);
    }
}
function add_form(){
    echo "<form action=\"mod=panel&file=contents\" method=\"post\" enctype=\"multipart/form-data\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td>Id</td><td><input type=\"text\" name=\"id\" class=\"textbox\"/> </td></tr>
<tr><td>Channel Id</td><td><input type=\"text\" name=\"channel_id\" class=\"textbox\"/> </td></tr>
<tr><td>Title</td><td><input type=\"text\" name=\"title\" class=\"textbox\"/> </td></tr>
<tr><td>Media Embed</td><td><input type=\"text\" name=\"media_embed\" class=\"textbox\"/> </td></tr>
<tr><td>Media File</td><td><input type=\"text\" name=\"media_file\" class=\"textbox\"/> </td></tr>
<tr><td>Description</td><td><input type=\"text\" name=\"description\" class=\"textbox\"/> </td></tr>
<tr><td>Hits</td><td><input type=\"text\" name=\"hits\" class=\"textbox\"/> </td></tr>
<tr><td>Hit Like</td><td><input type=\"text\" name=\"hit_like\" class=\"textbox\"/> </td></tr>
<tr><td>Hit Dislike</td><td><input type=\"text\" name=\"hit_dislike\" class=\"textbox\"/> </td></tr>
<tr><td>Date Create</td><td><input type=\"text\" name=\"date_create\" class=\"textbox\"/> </td></tr>
<tr><td>User Create</td><td><input type=\"text\" name=\"user_create\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function add_process(){
    global $db,$id,$channel_id,$title,$media_embed,$media_file,$description,
            $hits,$hit_like,$hit_dislike,$user_id;
    if (!empty($title)){
        $sql="insert into _channel_contents values (null,$channel_id,'$title',
        '$media_embed','$media_file',
        '$description',0,0,0,sysdate(),$user_id)";
        $db->query($sql);
        refresh_to("?mod=panel&file=channel", "Kembali ke daftar Channel", 1);
    }
}
function delete(){
    global $db,$id;
    $channel_name=get_channel_info($id, 'channel_name');
    $msg= "Channel $channel_name is deleted!";
    $db->query("delete from _channel_channels where id=$id");
    refresh_to("?mod=panel&file=channel", $msg, 2);
}
function play_media(){
    global $content_id;
    echo create_media_player($content_id);
}
global $action;

switch($action){
    case 'add_process':
        add_process();
        break;
    case 'add':
        add_form();
        break;
    case 'edit':
        edit_form();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete();
        break;
    case 'play_media':
        play_media();
        break;
    default:
        list_table();
        break;
}
?>
<p align="center">
<a href="javascript:history.go(-1);"><img src="images/back_button.png"/></a></p>